<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>

<%@ page import="javax.jdo.Query" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="java.util.List" %>

<%@ page import="connectfour.PMF" %>
<%@ page import="connectfour.Game" %>
<%@ page import="connectfour.Player" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QUAD ATTACK! - Lobby</title>
</head>
<body>
<%
	PersistenceManager pm = PMF.get().getPersistenceManager();

	// get current user
	UserService userService = UserServiceFactory.getUserService();
	User user = userService.getCurrentUser();
	
	// get the player entity that matches the logged in user
	Query query1 = pm.newQuery(Player.class);
	query1.declareImports("import com.google.appengine.api.users.User");
	query1.setFilter("player == playerParam");
	query1.setUnique(true);
	query1.declareParameters("User playerParam");
	
	Player player;
	
	try {
		Player result = (Player) query1.execute(user);
		if (result != null) {
			player = result;
		} else {
			// if there is no corresponding player entity for this user, then create one
			player = new Player(user);
			pm.makePersistent(player);
		}
	} finally {
		query1.closeAll();
	}
%>
	<center>
		<h1>Welcome to the Lobby</h1>
	</center>
	<% if(player.getStatus() == 0) { %>
		<center>
			<form action="/create" method="post">
			<input type="submit"value="Host Game">
			</form>
		</center>
		<% 
	    	String query = "SELECT FROM connectfour.Game WHERE over == false";
	    	List<Game> openGames = (List<Game>) pm.newQuery(query).execute();
	    %>
	    
	    <% if(openGames.isEmpty()){ %>
			<p>There are no open games.  Try starting your own!</p>
		<% } else{ %>
			<table>
			<% for(Game g:openGames){ %>
				<%
					Player host;
					
					try {
						host = pm.getObjectById(Player.class, g.getHost());
					} finally {
					} 
				%>
				
				<tr>
				<td>
					<form action="/join?gameID=<%=g.getKey().getId()%>" method="post">
					<input type="submit"value="Join Game">
					</form>
				</td>
				<td>
				<td>[Host: <%=host.getPlayer()%>] </td>
				<td>[Game Status: Open] </td>
				<td>[Game ID: <%= g.getKey() %>]</td>
				</tr>
			<% } %>
			</table>
		<% } %>
	<% }else{ %>
		<%
			Query query2 = pm.newQuery(Game.class);
			query2.declareImports("import com.google.appengine.api.datastore.Key");
			query2.setFilter("done == false");
			
			if(player.getStatus() == 1) query2.setFilter("host == keyParam");
			else query2.setFilter("opponent == keyParam");
			
			query2.setUnique(true);
			query2.declareParameters("Key keyParam");
			
			Game game;
			
			try {
				Game result = (Game) query2.execute(player.getKey());
				game = result;  // should never be null
			} finally {
				query2.closeAll();
			} 
		%>
		
		<center>
			You are already in a game. <br />
			Click <a href="/game.jsp?gameID=<%= game.getKey().getId() %>">here</a> to rejoin it<br />
			or here to leave it.
		</center>
	<% } %>
</body>
</html>